package Test_2019_2_20;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * 用java IO实现：将一个目录下的文件copy到另一个目录下。
 * a)	输入：源目录，目标目录
 * b)	输出：文件及目录结构复制成功并打印复制的文件个数
 */
public class FileCopy {
    private File file = null;
    private String sourceDir = null;
    private String signDir = null;
    private boolean flag = false;

    public static void main(String[] args) {
        new FileCopy();
    }

    public FileCopy() {
        inputSourceDir();
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isFile()) System.out.println(f + "\t这是一个文件。");
            else if (f.isDirectory()) System.out.println(f + "\t这是一个目录。");
        }
        inputSignDir();
        copyFile(files);
    }

    private void inputSourceDir() {
        flag = false;
        do {
            System.out.println("请输入源目录：");
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            try {
                sourceDir = br.readLine();
                file = new File(sourceDir);
                if (file.isDirectory()) {
                    flag = true;
                } else {
                    flag = false;
                }
            } catch (IOException e) {
                System.out.println("输入错误，请重新输入。");
            }
        } while (false == flag);
    }

    private void inputSignDir() {
        flag = false;
        do {
            System.out.println("请输入目标目录：");
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            try {
                signDir = br.readLine();
                file = new File(signDir);
                if (file.isDirectory()) {
                    flag = true;
                } else {
                    flag = false;
                }
            } catch (IOException e) {
                System.out.println("输入错误，请重新输入。");
            }
        } while (false == flag);
    }

    private void copyFile(File[] files) {
        System.out.println("拷贝的源文件夹路径：" + sourceDir);
        System.out.println("拷贝的目标文件夹路径：" + signDir);
        for (File f : files) {
            try {
                File ok = new File(signDir, f.getName());
                if (false == ok.exists()) {
                    ok.createNewFile();
                }
                FileInputStream fis = new FileInputStream(f);
                byte[] size = new byte[(int) f.length()];
                fis.read(size);
                FileOutputStream fos = new FileOutputStream(ok);
                fos.write(size);
                if ((f.getName().equals(ok.getName())) && (f.length() == ok.length())) {
                    System.out.println(f + "\t文件拷贝成功！");
                } else {
                    System.out.println(f + "\t文件拷贝失败！");
                }
                fis.close();
                fos.close();
            } catch (FileNotFoundException e) {
                System.out.println("文件（夹）没有找到！");
            } catch (IOException e) {
                System.out.println("文件读取出错！");
            }
        }
    }
}
